﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NearForums.DataAccess.Xml
{
    class TopicsSubscriptionsDataAccess : BaseDataAccess, ITopicsSubscriptionsDataAccess
    {
        public void Add(int topicId, int userId)
        {
            string filePath = string.Format(GetDataPath(TopicFile), topicId);
            XDocument doc = XDocument.Load(filePath);
            var subElem = doc.Root.Element("Subscriptions");
            var qry = from s in subElem.Elements("Subscription")
                      where (int)s == userId
                      select s;
            XElement elem = qry.SingleOrDefault();
            if (elem == null)
            {
                elem = new XElement("Subscription", userId);
                subElem.Add(elem);
                doc.Save(filePath);
            }

            string filePath2 = GetDataPath(UsersFile);
            XDocument doc2 = XDocument.Load(filePath2);
            var qry2 = from u in doc2.Root.Elements("User")
                       where (int)u.Element("UserId") == userId
                       select u;

            var elem2 = qry2.SingleOrDefault();
            if (elem2 != null)
            {
                var subElem2 = elem2.Element("Subscriptions");
                var qry3 = from s in subElem2.Elements("Subscription")
                           where (int)s == topicId
                           select s;

                XElement elem3 = qry3.SingleOrDefault();
                if (elem3 == null)
                {
                    elem3 = new XElement("Subscription", topicId);
                    subElem2.Add(elem3);
                    doc2.Save(filePath2);
                }
            }
        }

        public List<Topic> GetTopicsByUser(int userId)
        {
            string filePath = GetDataPath(UsersFile);
            XDocument doc = XDocument.Load(filePath);
            var qry = from u in doc.Root.Elements("User")
                      where (int)u.Element("UserId") == userId
                      select u;
            XElement elem = qry.SingleOrDefault();
            List<Topic> topics = new List<Topic>();
            if (elem != null)
            {
                foreach (var subElem in elem.Element("Subscriptions").Elements("Subscription"))
                {
                    TopicsDataAccess tda = new TopicsDataAccess();
                    topics.Add(tda.Get((int)subElem));
                }
            }
            return topics;
        }

        public List<User> GetUsersByTopic(int topicId)
        {
            string filePath = string.Format(GetDataPath(TopicFile), topicId);
            XDocument doc = XDocument.Load(filePath);

            List<User> users = new List<User>();
            foreach (var subElem in doc.Root.Element("Subscriptions").Elements("Subscription"))
            {
                UsersDataAccess uda = new UsersDataAccess();
                users.Add(uda.Get((int)subElem));
            }

            return users;
        }

        public int Remove(int topicId, int userId, Guid guid)
        {
            // Why is a guid required?
            string filePath = string.Format(GetDataPath(TopicFile), topicId);
            XDocument doc = XDocument.Load(filePath);
            var qry = from s in doc.Root.Element("Subscriptions").Elements("Subscription")
                      where (int)s == userId
                      select s;
            var subElem = qry.SingleOrDefault();
            if (subElem != null)
            {
                subElem.Remove();
                doc.Save(filePath);
            }

            string filePath2 = GetDataPath(UsersFile);
            XDocument doc2 = XDocument.Load(filePath2);
            var qry2 = from u in doc2.Root.Elements("User")
                       where (int)u.Element("UserId") == userId
                       select u;

            var userElem = qry2.SingleOrDefault();
            if (userElem != null)
            {
                var qry3 = from s in userElem.Element("Subscriptions").Elements("Subscription")
                           where (int)s == topicId
                           select s;
                var subElem2 = qry3.SingleOrDefault();
                if (subElem2 != null)
                {
                    subElem2.Remove();
                    doc2.Save(filePath2);
                }
            }
            return 0;
        }
    }
}
